/*----------------------------------------\ | Capitalize the 1st letter of a sentence or the 1st letter of each | | word in a setence; and parse a string variable; | |-------------------------------------------| |--------------------------------------------------------------------| |---------------------------| | indata: input data set; | | var: the variable need to be capitalized; | | outdata: if an outdata is given, then the output the dataset to it;| | otherwise saved the dataset to the input dataset; | | dlm: the delimiter to separate word or setence; | | if dlm=' ' or WORD, capitalize the 1st letter of each word in | | the variable; | | if dlm=';', ',' or SENTENCE, capitalize the 1st letter of the | | sentence; | | if dlm=any punctuation, capitalize the 1st letter after it; | |-----------------------------| |--------------------------------------------------------------------| |---------------------------------------| | Example1: | | data one; | | input x y cpevent $21.; | | datalines; | | 1.1 1.2 'OneTwo' | | 12 132 tHIS is, AN,EXAMPLE | | 1.1 2.3 1 MONTH FOLLOW-UP | | 2.1 232 4 MONTH FOLLOW-UP | | 1.3 4.1 7 MONTH FOLLOW-UP | | 4.2 100 UNSCHEDULED FOLLOW-UP | | ; | | data two; | | set one; | | cpevent=lowcase(cpevent); | | dvstr(var=cpevent, | | dlm=',' ';' '/' '\' '"' "'" '(' ')' ' ' '-', | | except=('an', 'a')); | | run; %print(two); | | Example2: %dvstr(var=evntdes, dlm=(' ', '.', ' '), except=('S/F'));| | Usage: dvstr(var=, dlm=' ', except=); | \----------------------------------------*/ %macro dvstr/parmbuff; /*--------------------------------------------\ | Copy Right: Duo Zhou; | | Created: 12-27-2001 12:21am; | | Purpose: Capitalize the 1st letter of a | | sentence or the 1st letter of each | | word in a setence; | \--------------------------------------------*/ %local var dlm except _dvupi_ _argtmp_; %let var=; %let dlm=; %let except=; %let syspbuff=%sysfunc(translate(%quote(%substr(%quote(%trim(%quote(%left(%quote(&syspbuff))))), 2, %eval(%length(%trim(%quote(%left(%quote(&syspbuff)))))-2))), %str(%'), %str(%"))); %let _dvstrrx_=%sysfunc(rxparse($(1))); %let _xdvstrpos_=0; %let _xdvstrlen_=0; %let _xdvstroldstr_=; %let _xdvstrnewstr_=; %do %while( %sysfunc(rxmatch(&_dvstrrx_, %quote(&syspbuff))) ); %syscall rxsubstr(_dvstrrx_, syspbuff, _xdvstrpos_, _xdvstrlen_); %let _xdvstroldstr_=%quote(%substr(%quote(&syspbuff), &_xdvstrpos_, &_xdvstrlen_)); %let _xdvstrnewstr_=%quote(%sysfunc(translate(%quote(&_xdvstroldstr_), À, %quote(%(), Á, %quote(%)), ´, %quote(,), ®, %quote( )))); %let syspbuff=%sysfunc(tranwrd(%quote(&syspbuff), %quote(&_xdvstroldstr_), %quote(&_xdvstrnewstr_))); %let _xdvstrpos_=0; %let _xdvstrlen_=0; %let _xdvstroldstr_=; %let _xdvstrnewstr_=; %end; %let _dvstrnewstr_=&syspbuff; %local _xdvstrvarcnt_ _xdvstrvar_; %let _xdvstrvarcnt_=0; %do %while(%length(%qscan(%nrbquote(&_dvstrnewstr_), %eval(&_xdvstrvarcnt_+1), %nrbquote(,)))); %let _xdvstrvarcnt_=%eval(&_xdvstrvarcnt_+1); %let _xdvstrvar_=%nrbquote(%qscan(%nrbquote(&_dvstrnewstr_), &_xdvstrvarcnt_, %nrbquote(,))); %let _xdvstrvar_=%sysfunc(translate(%quote(&_xdvstrvar_), '(', 'À', ')', 'Á', ',', '´', ' ', '®')); %let _xdvstrx2_=%trim(%left(%qscan(%quote(&_xdvstrvar_), 1, %str(=)))); %let _xdvstrx3_=%substr(%quote(&_xdvstrvar_), %eval(%index(%quote(&_xdvstrvar_),%str(=))+1), %eval(%length(&_xdvstrvar_)-%index(%quote(&_xdvstrvar_),%str(=)))); %if (not %index(%BQUOTE(%trim(%BQUOTE(%left(%BQUOTE(&_xdvstrvar_))))), %str(=))) %then %do; %if (%index(%BQUOTE(%trim(%BQUOTE(%left(%BQUOTE(&_xdvstrx2_))))), %str(%()) eq 1) and (%index(%BQUOTE(%trim(%BQUOTE(%left(%BQUOTE(%sysfunc(reverse(&_xdvstrx2_))))))), %str(%))) eq 1) %then %let _xdvstrx3_=%substr(%quote(%trim(%quote(%left(%quote(&_xdvstrx2_))))), 2, %eval(%length(%trim(%quote(%left(%quote(&_xdvstrx2_)))))-2)); %if (%quote(&_xdvstrvarcnt_) = %quote(1)) %then %let var=&_xdvstrx3_; %else %if (%quote(&_xdvstrvarcnt_) = %quote(2)) %then %let dlm=&_xdvstrx3_; %else %if (%quote(&_xdvstrvarcnt_) = %quote(3)) %then %let except=&_xdvstrx3_; %end; %else %if (%index(%BQUOTE(%trim(%BQUOTE(%left(%BQUOTE(&_xdvstrx3_))))), %str(%()) eq 1) and (%index(%BQUOTE(%trim(%BQUOTE(%left(%BQUOTE(%sysfunc(reverse(&_xdvstrx3_))))))), %str(%))) eq 1) and (%index(%nrbquote(upcase(%nrbquote(%sysfunc(compress(%nrbquote(&_xdvstrx2_)))))), WHERE=) le 1) %then %let &_xdvstrx2_=%substr(%quote(%trim(%quote(%left(%quote(&_xdvstrx3_))))), 2, %eval(%length(%trim(%quote(%left(%quote(&_xdvstrx3_)))))-2)); %else %let &_xdvstrx2_=&_xdvstrx3_; %end; length _last1char_ _last2char_ _dvchar_ $1.; _NEWCP_=&var||&var; %if (%quote(&except) ne) %then %if (%index(%BQUOTE(%trim(%BQUOTE(%left(%BQUOTE(&except))))), %str(%()) eq 1) and (%index(%BQUOTE(%trim(%BQUOTE(%left(%BQUOTE(%sysfunc(reverse(&except))))))), %str(%))) eq 1) %then %let except=%substr(%quote(%trim(%left(&except))), 2, %eval(%length(%quote(%trim(%left(&except))))-2)); %let _excpti_=0; %let _excptcnt_=0; &var=trimn(left(compbl(&var))); %do %while(%length(%nrbquote(%qscan(%nrbquote(&except), %eval(&_excpti_+1), %str(,))))); %let _excpti_=%eval(&_excpti_+1); %let _excptmp_=%qscan(%nrbquote(&except), &_excpti_, %str(,)); %if (%quote(%sysfunc(dequote(%quote(&_excptmp_)))) ne) %then %do; %let _excptmp_=%sysfunc(tranwrd(%nrbquote(&_excptmp_), ÌÎÍ, ',')); %let _excptfr&_excpti_=%sysfunc(dequote(&_excptmp_)); %let _excptto&_excpti_=®%trim(%left(&_excpti_)); &var=tranwrd(compbl(&var), "&&_excptfr&_excpti_", "&&_excptto&_excpti_"); %end; %else %let _excptfr&_excpti_=; %end; %let _excptcnt_=&_excpti_; %if (%quote(&dlm) eq ) %then %let dlm=' '; _last1char_=' '; _last2char_=' '; _NEWCP_=' '; _dummyqcp_=trimn(left(compress(&var, '"'))); _dummyqcp_=trimn(left(compress(_dummyqcp_, "'"))); _dummyqn_=length(trimn(left(&var)))-length(trimn(left(_dummyqcp_))); %if (%length(&dlm)>1) %then %do; retain _dummyqi_; _dummyqi_=0; do _dvari_=1 to length(&var); _dvchar_=substr(&var,_dvari_,1); if ((_last1char_ in (&dlm)) or (_last1char_=' ' and _last2char_ in (&dlm)) or (_dvari_=1)) then _dvchar_=upcase(_dvchar_); else _dvchar_=lowcase(_dvchar_); if (_last1char_ in (' ', '`', '~', '!', '@','#','$', '%', '^', '&','*','(',')','-','_','+','=','[',']','{','}','|','\',';',':','"',"'",'<','>',',','.','/','?') or (_last2char_ in (' ', '`', '~', '!', '@','#','$', '%', '^', '&','*','(',')','-','_','+','=','[',']','{','}','|','\',';',':','"',"'",'<','>',',','.','/','?')) or (_dvchar_ in (' ', '`', '~', '!', '@','#','$', '%', '^', '&','*','(',')','-','_','+','=','[',']','{','}','|','\',';',':','"',"'",'<','>',',','.','/','?'))) then do; if (_dvchar_ in ('`', '~', '!', '@','#','%', '^', ')','-','_','+','=',']','}','\',';',':','<','>',',','.','/','?')) then _NEWCP_=trimn(left(_NEWCP_))||_dvchar_; else if (_dvchar_ in ('$', '&','(','[','{','|')) then _NEWCP_=compbl(_NEWCP_)||_dvchar_; else if (_last1char_ in ('!','%', '&',')',']','}',';',':',',','.','?')) then _NEWCP_=compbl(_NEWCP_)||_dvchar_; else if (_last1char_=' ' and _last2char_ in ('`','~','@','#','$','^','*','(','[','{','\','<','>','/')) then _NEWCP_=trimn(left(_NEWCP_))||_dvchar_; else if (_last2char_ in ('!', '%', '&',')',']','}',';',':',',','.','?') and _last1char_=' ') then _NEWCP_=compbl(_NEWCP_)||_dvchar_; else if (_dvchar_ in ('"', "'")) then do; _dummyqi_=_dummyqi_+1; if (mod(_dummyqn_, 2)) then _NEWCP_=trimn(left(_NEWCP_))||_dvchar_; else if (mod(_dummyqi_, 2)) then _NEWCP_=compbl(_NEWCP_)||_dvchar_; else _NEWCP_=trimn(left(_NEWCP_))||_dvchar_; /* put '5 ' _dummyqi_= _NEWCP_=; */ end; else if (_last1char_ ne ' ' and _dvchar_ ne ' ') then _NEWCP_=trimn(left(_NEWCP_))||_dvchar_; else _NEWCP_=compbl(_NEWCP_)||_dvchar_; end; else _NEWCP_=trimn(left(_NEWCP_))||_dvchar_; _last2char_=_last1char_; _last1char_=_dvchar_; end; %end; %else _NEWCP_=upcase(substr(&var,1,1))||lowcase(substr(&var,2,(length(&var)-1)));; %if (%quote(&_excptcnt_) ge 1) %then %do; %do _excpti_=1 %to &_excptcnt_; %if (%quote(&&_excptfr&_excpti_) ne) %then _NEWCP_=tranwrd(_NEWCP_, "&&_excptto&_excpti_", "&&_excptfr&_excpti_");; %end; %end; &var=_NEWCP_; drop _NEWCP_ _dvari_ _dvchar_ _last1char_ _last2char_ _dummyqn_ _dummyqi_ _dummyqcp_; %mend dvstr;